//
//  TCPCMDType.swift
//  TimeBoat
//
//  Created by Jun on 2022/9/8.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import Foundation

@objcMembers class TCPCMDType: NSObject {
    public static let LK = "LK"
    public static let COORDINATE = "COORDINATE"
    public static let STEP = "STEP"
    public static let HEART_RATE = "HEARTRATE"
    public static let TEMP = "TEMP"
    public static let TOTAL_STEP = "TOTALSTEP"
    public static let OXYGEN = "OXYGEN"
    public static let SLEEP = "SLEEP"
    public static let RUN = "RUN"
    public static let HIKING = "HIKING"
    public static let RIDE = "RIDE"
    public static let MOUNTAINEERING = "MOUNTAINEERING"
    public static let RUNNINGMACHINE = "RUNNINGMACHINE"
    public static let BADMINTON = "BADMINTON"
    public static let BASKETBALL = "BASKETBALL"
    public static let FOOTBALL = "FOOTBALL"
    private static let HEADER = TCPCMDHeaderType.H_SGZ_A
}

extension TCPCMDType {
    public static func buildLKCommand(imei: String?,
                                      userUUID: String?) -> Data? {
        if nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0 {
            let model = TCPCMDUtil.shared().buildCommandModel(HEADER, imei: imei!, cmdType: LK, data: [userUUID!])
            
            return TCPCMDUtil.shared().buildCommandData(model)
        }
        
        return nil
    }
    
    public static func buildCoordinateCommand(imei: String?,
                                              userUUID: String?,
                                              deviceTimestampSecond: UInt32,
                                              lng: String?,
                                              lat: String?) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0
            && nil != lng
            && nil != lat) {
            let dataList = [userUUID!, lng!, lat!]
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei, cmdType: COORDINATE, data: dataList)
        }
        
        return nil;
    }
    
    public static func buildStepCommand(imei: String?,
                                        userUUID: String?,
                                        deviceTimestampSecond: UInt32,
                                        stepCount: UInt32,
                                        distance: UInt32,
                                        cal: UInt32) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            let dataList: [String] = [
                userUUID!,
                UInt32.toString(deviceTimestampSecond),
                UInt32.toString(stepCount),
                UInt32.toString(distance),
                UInt32.toString(cal)
            ]
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei!, cmdType:STEP, data:dataList)
        }
        
        return nil
    }
    
    public static func buildHeartRateCommand(imei: String?,
                                             userUUID: String?,
                                             deviceTimestampSecond: UInt32,
                                             heartRate: UInt32) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            let dataList: [String] = [
                userUUID!,
                UInt32.toString(deviceTimestampSecond),
                UInt32.toString(heartRate)
            ]
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei!, cmdType: HEART_RATE, data: dataList)
        }
        
        return nil
    }
    
    public static func buildTempCommand(imei: String?,
                                        userUUID: String?,
                                        deviceTimestampSecond: UInt32,
                                        temp: String?) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            let dataList: [String] = [
                userUUID!,
                UInt32.toString(deviceTimestampSecond),
                temp!
            ]
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei!, cmdType: TEMP, data: dataList)
        }
        
        return nil;
    }
    
    public static func buildTotalStepCommand(imei: String?,
                                             userUUID: String?,
                                             deviceTimestampSecond: UInt32,
                                             stepCount: UInt32,
                                             distance: UInt32,
                                             cal: UInt32) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            let dataList: [String] = [
                userUUID!,
                UInt32.toString(deviceTimestampSecond),
                UInt32.toString(stepCount),
                UInt32.toString(distance),
                UInt32.toString(cal)
            ]
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei!, cmdType: TOTAL_STEP, data: dataList)
        }
        
        return nil;
    }
    
    public static func buildOxygenCommand(imei: String?,
                                          userUUID: String?,
                                          deviceTimestampSecond: UInt32,
                                          bloodOxygen: UInt32) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            let dataList: [String] = [
                userUUID!,
                UInt32.toString(deviceTimestampSecond),
                UInt32.toString(bloodOxygen)
            ]
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei:imei!, cmdType: OXYGEN, data: dataList)
        }
        
        return nil
    }
    
    public static func buildSleepCommand(imei: String?,
                                         userUUID: String?,
                                         deviceTimestampSecond: UInt32,
                                         hour: UInt32,
                                         minute: UInt32,
                                         state: UInt32) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            let dataList: [String] = [
                userUUID!,
                UInt32.toString(deviceTimestampSecond),
                UInt32.toString(hour),
                UInt32.toString(minute),
                UInt32.toString(state)
            ]
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei!, cmdType: SLEEP, data: dataList)
        }
        
        return nil
    }
    
    public static func buildRunCommand(imei: String?,
                                       userUUID: String?,
                                       deviceTimestampSecond: UInt32,
                                       model: RunDataModel) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            var dataList: [String] = [String]()
            dataList.append(userUUID!);
            dataList.append(UInt32.toString(deviceTimestampSecond))
            /// 模式
            dataList.append(UInt8.toString(model.model))
            /// 总计步
            dataList.append(UInt32.toString(model.stepCount))
            /// 实际里程(米)
            dataList.append(UInt32.toString(model.actualDistance))
            /// 实际用时(秒)
            dataList.append(UInt32.toString(model.actualUseTime))
            /// 卡路里(卡)
            dataList.append(UInt32.toString(model.calorie))
            /// 配速长度
            /// 配速...(秒/公里)
            if (nil != model.paceList && model.paceList!.count > 0) {
                let paceList = model.paceList!
                dataList.append(Int.toString(paceList.count))
                for pace in paceList {
                    dataList.append(UInt16.toString(pace))
                }
            } else {
                dataList.append("0")
            }
            
            /// 心率长度
            /// 心率...
            if (nil != model.heartRateList && model.heartRateList!.count > 0) {
                let heartRateList = model.heartRateList!
                dataList.append(Int.toString(heartRateList.count))
                for i in heartRateList {
                    dataList.append(UInt8.toString(i))
                }
            } else {
                dataList.append("0")
            }
            
            /// 步频长度
            /// 步频...
            if (nil != model.stepFrequencyList && model.stepFrequencyList!.count > 0) {
                let stepFrequencyList = model.stepFrequencyList!
                dataList.append(Int.toString(stepFrequencyList.count))
                for i in stepFrequencyList {
                    dataList.append(UInt16.toString(i))
                }
            } else {
                dataList.append("0")
            }
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei!, cmdType: RUN, data: dataList)
        }
        
        return nil
    }
    
    public static func buildHikingCommand(imei: String?,
                                          userUUID: String?,
                                          deviceTimestampSecond: UInt32,
                                          model: HikingDataModel) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            var dataList: [String] = [String]()
            dataList.append(userUUID!);
            dataList.append(UInt32.toString(deviceTimestampSecond))
            /// 总计步
            dataList.append(UInt32.toString(model.stepCount))
            /// 实际里程(米)
            dataList.append(UInt32.toString(model.distance))
            /// 实际用时(秒)
            dataList.append(UInt32.toString(model.useTime))
            /// 卡路里(卡)
            dataList.append(UInt32.toString(model.calorie))
            /// 配速长度
            /// 配速...(秒/公里)
            if (nil != model.paceList && model.paceList!.count > 0) {
                let paceList = model.paceList!
                dataList.append(Int.toString(paceList.count))
                for pace in paceList {
                    dataList.append(UInt16.toString(pace))
                }
            } else {
                dataList.append("0")
            }
            /// 心率长度
            /// 心率...
            if (nil != model.heartRateList && model.heartRateList!.count > 0) {
                let heartRateList = model.heartRateList!
                dataList.append(Int.toString(heartRateList.count))
                for i in heartRateList {
                    dataList.append(UInt8.toString(i))
                }
            } else {
                dataList.append("0")
            }
            /// 步频长度
            /// 步频...
            if (nil != model.stepFrequencyList && model.stepFrequencyList!.count > 0) {
                let stepFrequencyList = model.stepFrequencyList!
                dataList.append(Int.toString(stepFrequencyList.count))
                for i in stepFrequencyList {
                    dataList.append(UInt16.toString(i))
                }
            } else {
                dataList.append("0")
            }
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei!, cmdType: HIKING, data: dataList)
        }
        
        return nil
    }
    
    public static func buildRideCommand(imei: String?,
                                        userUUID: String?,
                                        deviceTimestampSecond: UInt32,
                                        model: RideDataModel) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            var dataList: [String] = [String]()
            dataList.append(userUUID!);
            dataList.append(UInt32.toString(deviceTimestampSecond))
            /// 模式
            dataList.append(UInt8.toString(model.model))
            /// 实际里程(米)
            dataList.append(UInt32.toString(model.actualDistance))
            /// 实际用时(秒)
            dataList.append(UInt32.toString(model.actualUseTime))
            /// 卡路里(卡)
            dataList.append(UInt32.toString(model.calorie))
            /// 配速长度
            /// 配速...(秒/公里)
            if (nil != model.paceList && model.paceList!.count > 0) {
                let paceList = model.paceList!
                dataList.append(Int.toString(paceList.count))
                for pace in paceList {
                    dataList.append(UInt16.toString(pace))
                }
            } else {
                dataList.append("0")
            }
            /// 心率长度
            /// 心率...
            if (nil != model.heartRateList && model.heartRateList!.count > 0) {
                let heartRateList = model.heartRateList!
                dataList.append(Int.toString(heartRateList.count))
                for heartRate in heartRateList {
                    dataList.append(UInt8.toString(heartRate))
                }
            } else {
                dataList.append("0")
            }
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei!, cmdType: RIDE, data: dataList)
        }
        
        return nil
    }
    
    public static func buildMountaineeringCommand(imei: String?,
                                                  userUUID: String?,
                                                  deviceTimestampSecond: UInt32,
                                                  model: MountaineeringDataModel) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            var dataList: [String] = [String]()
            dataList.append(userUUID!)
            dataList.append(UInt32.toString(deviceTimestampSecond))
            /// 总计步
            dataList.append(UInt32.toString(model.stepCount))
            /// 实际里程(米)
            dataList.append(UInt32.toString(model.distance))
            /// 实际用时(秒)
            dataList.append(UInt32.toString(model.useTime))
            /// 卡路里(卡)
            dataList.append(UInt32.toString(model.calorie))
            /// 心率长度
            /// 心率...
            if (nil != model.heartRateList && model.heartRateList!.count > 0) {
                let heartRateList = model.heartRateList!
                dataList.append(Int.toString(heartRateList.count))
                for heartRate in heartRateList {
                    dataList.append(UInt8.toString(heartRate))
                }
            } else {
                dataList.append("0")
            }
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei!, cmdType: MOUNTAINEERING, data: dataList)
        }
        
        return nil
    }
    
    public static func buildRunningMachineCommand(imei: String?,
                                                  userUUID: String?,
                                                  deviceTimestampSecond: UInt32,
                                                  model: RunningMachineDataModel) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            var dataList: [String] = [String]()
            dataList.append(userUUID!)
            dataList.append(UInt32.toString(deviceTimestampSecond))
            /// 总计步
            dataList.append(UInt32.toString(model.stepCount))
            /// 实际用时(秒)
            dataList.append(UInt32.toString(model.useTime))
            /// 卡路里(卡)
            dataList.append(UInt32.toString(model.calorie))
            /// 心率长度
            /// 心率...
            if (nil != model.heartRateList && model.heartRateList!.count > 0) {
                let heartRateList = model.heartRateList!
                dataList.append(Int.toString(heartRateList.count))
                for heartRate in heartRateList {
                    dataList.append(UInt8.toString(heartRate))
                }
            } else {
                dataList.append("0")
            }
            /// 步频长度
            /// 步频...
            if (nil != model.stepFrequencyList && model.stepFrequencyList!.count > 0) {
                let stepFrequencyList = model.stepFrequencyList!
                dataList.append(Int.toString(stepFrequencyList.count))
                for step in stepFrequencyList {
                    dataList.append(UInt16.toString(step))
                }
            } else {
                dataList.append("0")
            }
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei!, cmdType: RUNNINGMACHINE, data: dataList)
        }
        
        return nil
    }
    
    public static func buildBadmintonCommand(imei: String?,
                                             userUUID: String?,
                                             deviceTimestampSecond: UInt32,
                                             model: BallDataModel) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            var dataList: [String] = [String]()
            dataList.append(userUUID!)
            dataList.append(UInt32.toString(deviceTimestampSecond))
            /// 实际用时(秒)
            dataList.append(UInt32.toString(model.useTime))
            /// 卡路里(卡)
            dataList.append(UInt32.toString(model.calorie))
            /// 心率长度
            /// 心率...
            if (nil != model.heartRateList && model.heartRateList!.count > 0) {
                let heartRateList = model.heartRateList!
                dataList.append(Int.toString(heartRateList.count))
                for heartRate in heartRateList {
                    dataList.append(UInt8.toString(heartRate))
                }
            } else {
                dataList.append("0")
            }
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei!, cmdType: BADMINTON, data: dataList)
        }
        
        return nil
    }
    
    public static func buildBasketballCommand(imei: String?,
                                              userUUID: String?,
                                              deviceTimestampSecond: UInt32,
                                              model: BallDataModel) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            var dataList: [String] = [String]();
            dataList.append(userUUID!);
            dataList.append(UInt32.toString(deviceTimestampSecond));
            /// 实际用时(秒)
            dataList.append(UInt32.toString(model.useTime))
            /// 卡路里(卡)
            dataList.append(UInt32.toString(model.calorie))
            /// 心率长度
            /// 心率...
            if (nil != model.heartRateList && model.heartRateList!.count > 0) {
                let heartRateList = model.heartRateList!
                dataList.append(Int.toString(heartRateList.count))
                for heartRate in heartRateList {
                    dataList.append(UInt8.toString(heartRate))
                }
            } else {
                dataList.append("0")
            }
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei!, cmdType: BASKETBALL, data: dataList);
        }
        
        return nil;
    }
    
    public static func buildFootballCommand(imei: String?,
                                            userUUID: String?,
                                            deviceTimestampSecond: UInt32,
                                            model: BallDataModel) -> Data? {
        if (nil != imei && imei!.count > 0
            && nil != userUUID && userUUID!.count > 0) {
            var dataList: [String] = [String]();
            dataList.append(userUUID!)
            dataList.append(UInt32.toString(deviceTimestampSecond))
            /// 实际用时(秒)
            dataList.append(UInt32.toString(model.useTime))
            /// 卡路里(卡)
            dataList.append(UInt32.toString(model.calorie))
            // 心率长度
            /// 心率...
            if (nil != model.heartRateList && model.heartRateList!.count > 0) {
                let heartRateList = model.heartRateList!
                dataList.append(Int.toString(heartRateList.count))
                for heartRate in heartRateList {
                    dataList.append(UInt8.toString(heartRate))
                }
            } else {
                dataList.append("0")
            }
            
            return TCPCMDUtil.shared().buildHeader(HEADER, imei: imei!, cmdType: FOOTBALL, data: dataList)
        }
        
        return nil
    }
}
